<?php

	// PASTT: PHP Android Translation Tool
	// http://code.google.com/p/android-php-translator/
	// Licensed Apache License 2.0
	// http://www.apache.org/licenses/LICENSE-2.0
	
	define('DIRECT_ACCESSIBLE', TRUE);
	include('includes/common.php');
	
	function zip($source, $destination) {
	    if (!extension_loaded('zip') || !file_exists($source)) {
    	    return false;
	    }

    	$zip = new ZipArchive();
	    if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
    	    return false;
	    }

    	$source = str_replace('\\', '/', realpath($source));

	    if (is_dir($source) === true) {
    	    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

	        foreach ($files as $file) {
    	        $file = str_replace('\\', '/', realpath($file));
	
	            if (is_dir($file) === true) {
        	        $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
    	        } else if (is_file($file) === true && basename($file) == 'strings.xml') {
                	$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
            	}
        	}
	    } else if (is_file($source) === true) {
        	$zip->addFromString(basename($source), file_get_contents($source));
    	}

    	return $zip->close();
	}
	
	// Download the translations
	if (isset($_POST['download'])) {
		$filename = 'res-' . time() . '.zip';
		$simple_filename = 'res.zip';
		zip($basedir, $filename);
	
		header('Pragma: public');
	    header('Expires: 0');
    	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	    header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filename)) . ' GMT');
    	header('Content-Type: application/force-download');
	    header('Content-Disposition: inline; filename="'. $simple_filename . '"');
    	header('Content-Transfer-Encoding: binary');
	    header('Content-Length: ' . filesize($filename));
    	header('Connection: close');
	    readfile($filename);
    	
	    unlink($filename);
	}
	
	// Save the uploaded strings.xml file
	if (isset($_POST['submit'])) {
		$target_path = $basedir . '/values/';
		$target_file = $target_path . 'strings.xml';
		
		if (file_exists($target_file)) {
			$target_file_old = $target_path . 'strings.old.xml';
			if (file_exists($target_file_old)) {
				unlink($target_file_old);
			}
			
			if (copy($target_file, $target_file_old)) {
				unlink($target_file);
			}
		}
		
		// Check if no target_file exists (the first time or it has been moved to old.xml)
		if (!file_exists($target_file)) {
			if(!move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_file)) {
				echo "There was an error uploading the strings.xml file, please try again!";
			}
		}
	}
	
	// Default language to select?
	include('includes/checklanguage.php');
	$defaultLanguage = getDefaultLanguage('aa');
	if (strlen($defaultLanguage) > 2) {
		// Remove regional identifier
		$defaultLanguage = substr($defaultLanguage, 0, 2);
	}

	$pageTitle = $appname;
	include('includes/header.php');

	echo '
	<h1>' . $appname . '</h1>';
	
	echo '
	<center>
	<table id="translationoverview">
		<tr>
			<th colspan="3">Existing translations</td>
		</tr>';
	
	$isuneven = false;
	$classuneven = ' class="uneven"';
	
	if (isset($languages)) {
		sort($languages);
		foreach ($languages as $language) {
			
			// Show the language and an edit link
			$langgroup = substr($language, 0, 2);
			echo '
		<tr' . ($isuneven? $classuneven: '') . '>
			<td id="lang">' . $language . '</td>
			<td id="langgroup">' . $iso639[$langgroup] . '</td>
			<td><a href="translation.php?lang=' . $language . '">Edit '. $iso639[$langgroup] . ' translation</a></td>
		</tr>';
		
			$isuneven = !$isuneven;
		}
	}
	
	if ($allowAddLanguage) {
		echo '
			<tr>
				<td colspan="3">
					<form id="addtranslation" name="addtranslation" method="GET" action="translation.php">
						Add a new translation for: 
						<select id="lang" name="lang">';
		foreach ($iso639 as $langcode => $langname) {
			$selected = '';
			if ($defaultLanguage == $langcode) {
				$selected = ' selected="selected"';
			}
			echo '
							<option value="' . $langcode . '"' . $selected . '>' . $langcode . ' - ' . $langname . '</option>';
		}
		echo '
						</select>
						<strong>-r</strong><input type="text" id="region" name="region" /> (optional region code)
						<input type="submit" id="submit" name="submit" value="Add" />
					</form>
				</td>
			</tr>';
	}
	
	if ($allowDownloadResources) {
		echo '
			<tr>
				<td colspan="3">
					<form action="index.php" method="POST">
						<input type="submit" id="download" name="download" value="Download Resources as Zip file" />
					</form>
				</td>
			</tr>';
	}
	
	if ($allowUploadStringsXml) {
		echo '<tr><th colspan="3">Upload new base file (Strings.xml)</td></tr>';
	
		echo '<tr><td colspan="3">
	
			<form enctype="multipart/form-data" action="index.php" method="POST">
				<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
				Choose a file to upload: <input name="uploadedfile" type="file" /> <input type="submit" id="submit" name="submit" value="Upload Strings.xml" />
			</form>
		
		</td></tr>';
	}
	
	echo '</table>
	</center>';
?>

<?php
	include('includes/footer.php');

?>